package li.brauch.semesterarbeit.radio.views;

import java.util.ArrayList;
import java.util.List;

import li.brauch.semesterarbeit.radio.views.util.ColorStore;
import li.brauch.semesterarbeit.radio.views.util.FontStore;
import li.brauch.semesterarbeit.radio.views.util.ImageStore;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;

public class SelectPanel extends Composite {
	
	private List<SelectPanelListener> listeners = new ArrayList<SelectPanelListener>();
	
	private CLabel label;
	private Combo combo;
	private Button button;
	
	public SelectPanel(Composite parent) {
		
		super(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.numColumns = 3;
		setLayout(layout);
		setBackground(ColorStore.BACKGROUND);
		
		GridData layoutdata = new GridData();
		layoutdata.horizontalAlignment = GridData.FILL;
		layoutdata.verticalAlignment = GridData.FILL;
		layoutdata.grabExcessHorizontalSpace = true;
		layoutdata.grabExcessVerticalSpace = false;
		setLayoutData(layoutdata);						
		
		label = new CLabel(this, SWT.LEFT);
		label.setBackground(ColorStore.BACKGROUND);
		label.setFont(FontStore.TITLE);
		label.setForeground(ColorStore.FOREGROUND);
		layoutdata = new GridData();
		layoutdata.horizontalSpan = 3;
		layoutdata.horizontalAlignment = GridData.FILL;
		layoutdata.grabExcessHorizontalSpace = true;
		layoutdata.verticalAlignment = GridData.BEGINNING;
		layoutdata.grabExcessVerticalSpace = false;		
		label.setLayoutData(layoutdata);
		
		combo = new Combo (this, SWT.READ_ONLY);
		layoutdata = new GridData();
		layoutdata.horizontalAlignment = GridData.FILL;
		layoutdata.grabExcessHorizontalSpace = true;
		layoutdata.verticalAlignment = GridData.BEGINNING;
		
		layoutdata.grabExcessVerticalSpace = false;		
		combo.setLayoutData(layoutdata);
		
		button = new Button(this, SWT.PUSH | SWT.FLAT);
		button.setBackground(ColorStore.BACKGROUND);
		button.setImage(ImageStore.CHECK);		
		layoutdata = new GridData();		
		layoutdata.horizontalAlignment = GridData.BEGINNING;
		layoutdata.grabExcessHorizontalSpace = true;
		layoutdata.verticalAlignment = GridData.BEGINNING;
		layoutdata.heightHint = 24;
		layoutdata.grabExcessVerticalSpace = true;		
		button.setLayoutData(layoutdata);
		
		button.addSelectionListener(new SelectionAdapter() {
			
			public void widgetSelected(SelectionEvent e) {
				if (combo.getSelectionIndex() == -1) {
					combo.setFocus();
					return;
				}
				notifyStationSelected(combo.getItem(combo.getSelectionIndex()));				
			}
		});
	}
	
	public void addSearchPanelListener(SelectPanelListener listener) {
		if (!listeners.contains(listener))
			listeners.add(listener);
	}
	
	public void removeSearchPanelListener(SelectPanelListener listener) {
		if (listeners.contains(listener))
			listeners.remove(listener);
	}
	
	private void notifyStationSelected(final String station) {
		for (SelectPanelListener listener : listeners) {
			listener.stationSelected(station);
		}	
	}
	
	public void setItems(String[] items) {
		combo.setItems(items);
	}
	
	public void setLabel(String text) {
		label.setText(text);
	}
}
